<template>
  <div class="scroll">
    <div class="content-main">这是一段内容</div>
    <div class="content-scroll">
      <div class="scroll-item" v-for="(item, index) in list" :key="index">
        <div class="scroll-item-title">
          {{ item.title }}
        </div>
        <div class="scroll-item-main">
          {{ item.content }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'

const list = ref([])
const loading = ref(false)

function getData() {
  if (loading.value) return
  loading.value = true
  axios('/1.json').then(res => {
    list.value = [...list.value, ...res.data]
    loading.value = false
  })
}

function scroll() {
  const scrollHeight = document.body.scrollHeight
  const scrollTop = document.body.scrollTop || document.documentElement.scrollTop
  const clientHeight = document.documentElement.clientHeight
  const distance = scrollHeight - scrollTop - clientHeight
  if (distance < 20) {
    getData()
  }
}

getData()
window.addEventListener('scroll', () => {
  scroll()
})
window.removeEventListener('scroll', () => {
  scroll()
})
</script>

<style scoped lang="less">
.scroll {
  display: flex;
  padding: 20px;
}
.content-main {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: skyblue;
  font-size: 30px;
  color: #fff;
}
.content-scroll {
  width: 400px;
  background-color: pink;
}
.scroll-item {
  width: 100%;
  box-sizing: border-box;
  padding: 20px;
  border-bottom: 1px solid #000;
}
.scroll-item-title {
  width: 100%;
  font-size: 18px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-weight: bold;
}
.scroll-item-main {
  width: 100%;
  font-size: 14px;
  line-height: 26px;
  overflow: hidden;
  margin-top: 10px;
  color: #666;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
